package com.cby.android.sample.fragments

import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.cby.android.library.base.BaseStateTabFragment
import com.cby.android.library.ext.observe
import com.cby.android.sample.intent.ProjectIntent
import com.cby.android.sample.intent.RollNewsTypeIntent
import com.cby.android.sample.model.RollNewsViewModel
import com.cby.android.sample.state.RollNewsTypeState
import kotlinx.coroutines.launch


class RollNewsFragment :
    BaseStateTabFragment<RollNewsViewModel, RollNewsTypeState, RollNewsTypeIntent>() {


    override fun reload() {
        lifecycleScope.launch {
            viewModel!!.intents.send(RollNewsTypeIntent.LoadRollNewsType)
        }
    }

    override fun loadData() {
        reload()
    }

    override fun bindLiveData() {
        observe(viewModel!!.state) {
            val classifyList = it.classify
            Log.i("chenby", "classifyList = ${classifyList.size}")
            for (classifyBean in classifyList) {
                pagerAdapter.addFragment(
                    RollNewsListFragment.newInstance(classifyBean.typeId),
                    classifyBean.typeName
                )
            }
            pagerAdapter.notifyDataSetChanged()
            viewPager.offscreenPageLimit = classifyList.size
            tabLayout.setupWithViewPager(viewPager)
        }
    }


}